﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utility;

namespace TimeTracker.Provider
{
    public static class IProjectExtensions
    {
        public static TimeSpan Duration(this IProject container)
        {
            TimeSpan timeSpan = new TimeSpan();

            timeSpan += container.Sessions.Duration();

            foreach (IProject project in container.Projects)
            {
                timeSpan += project.Duration();
            }

            return timeSpan;
        }

        public static TimeSpan Duration(this IProject container, DateTime day)
        {
            TimeSpan timeSpan = new TimeSpan();

            timeSpan += container.Sessions.ByDay(day).ThatAreNotArchived().Duration();

            foreach (IProject project in container.Projects)
            {
                timeSpan += project.Duration(day);
            }

            return timeSpan;
        }

        public static bool IsArchived(this IProject container)
        {
            bool archived = container.Sessions.AreArchived();

            if (archived)
            {
                foreach (IProject project in container.Projects)
                {
                    if (!project.IsArchived())
                    {
                        return false;
                    }
                }
            }

            return archived;
        }

        public static bool IsArchived(this IProject container, DateTime day)
        {
            bool archived = container.Sessions.ByDay(day).AreArchived();

            if (archived)
            {
                foreach (IProject project in container.Projects)
                {
                    if (!project.IsArchived(day))
                    {
                        return false;
                    }
                }
            }

            return false;
        }

        public static string FullName(this IProject container)
        {
            string name = container.Name;

            while (container.Project != null)
            {
                container = container.Project;

                name = container.Name + FullNameSeparater + name;
            }

            return name;
        }

        private static string FullNameSeparater = " > ";

        public static IEnumerable<IProject> AllProjects(this IProject project)
        {
            List<IProject> projects = new List<IProject>();

            projects.AddRange(project.Projects);

            foreach (IProject child in project.Projects)
            {
                projects.AddRange(child.AllProjects());
            }

            return projects;
        }

        public static IEnumerable<ISession> AllSessions(this IProject project)
        {
            List<ISession> sessions = new List<ISession>();

            sessions.AddRange(project.Sessions);

            foreach (IProject child in project.Projects)
            {
                sessions.AddRange(child.AllSessions());
            }

            return sessions;
        }
    }
}
